package com.engine.utilities;

import com.engine.Engine;

import android.util.Log;
import android.view.SurfaceHolder;

/**
 * User: cwatts
 * <p/>
 * Date: 12/29/11
 * <p/>
 * MainThread is used to
 */
public class MainThread extends Thread
{
	private static final String TAG = MainThread.class.getSimpleName();

	private boolean isRunning;

	private Engine engine;

	private MainGamePanel gamePanel;

	private SurfaceHolder surfaceHolder;

	/**
	 * Creates a new instance of the main thread.
	 * @param engine will be used in the thread to execute the update and render functions of the engine.
	 */
	public MainThread(Engine engine, SurfaceHolder surfaceHolder, MainGamePanel gamePanel)
	{
		super();
		this.isRunning = false;
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;
		this.engine = engine;
	}

	public boolean isRunning()
	{
		return isRunning;
	}

	public void setRunning(boolean running)
	{
		isRunning = running;
	}

	@Override
	/**
	 * This is the main thread loop for the engine.
	 */
	public void run()
	{
		long tickCount = 0L;

		Log.d(TAG, "Starting game thread");

		while(this.isRunning)
		{
			tickCount++;
			engine.Update();
			engine.Render();
		}

		Log.d(TAG, "Game loop executed " + tickCount + " times");
	}
}
